﻿package  {
	
	import flash.display.Sprite;
	import flash.display.BitmapData;
	
	public class TStage {
		private var _ObjArray: Vector.<TObject>;

		public function TStage() {
			_ObjArray = new Vector.<TObject>;
		}
		
		public function TestCollisionTest(): Vector.<TCollisionDetectorResultContainer> {
			var Result: Vector.<TCollisionDetectorResultContainer> = new Vector.<TCollisionDetectorResultContainer>;
			var TempArray: Vector.<TObject> = new Vector.<TObject>;
			var CollisionDetector: TCollisionDetector = new TCollisionDetector();
			
			for (var I = 0; I < Count; I++) {
				if (Get(I).Active) {
					TempArray[TempArray.length] = Get(I);
				}
			}
			
			if (Count >= 2) {
				for (I = 0; I < TempArray.length; I++) {
					for (var C: int = I; C < TempArray.length; C++) {
						if ((I != C) && ((TempArray[I].isMoving) || (TempArray[C].isMoving))) {
							if (CollisionDetector.HitTest(TempArray[I], TempArray[C])) {
								Result[Result.length] = new TCollisionDetectorResultContainer(TempArray[I], TempArray[C]);
							}
						}
					}
				}
			}
			
			return Result;
		}
		
		
		public function Add(Obj: TObject): int {
			_ObjArray[_ObjArray.length] = Obj;
			return _ObjArray.length - 1;
		}
		
		public function Create(Mass: Number, X: int, Y: int, Height: int, Width: int, BmpData: BitmapData): int {
			var _Obj = new TObject(Mass, X, Y, Height, Width, BmpData);
			return Add(_Obj);
		}
		
		public function Get(Index: int): TObject {
			if ((Index >= 0) && (Index < _ObjArray.length)) {
				return _ObjArray[Index];
			}
			return null;
		}
		
		public function Delete(Index: int) {
			_ObjArray.splice(Index, 1);
		}
		
		public function Destroy() {
			for (var I: int = 0; I < _ObjArray.length; I++) {
				_ObjArray[I] = null;
			}
			_ObjArray = null;
		}
		
		public function get Count(): int {
			return _ObjArray.length;
		}

	}
	
}
